﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package ZLocalization {
    import ZLocalization.Substituters.*;

    public class LocalizedString {

        public static const ORIGINAL:String = "original";

        protected static var PROPAGATED_ATTRIBUTES:Array = ["indefinite", "definite", "singular", "plural", "accusative"];

        protected var m_original:String;
        protected var m_variations:Object;
        protected var m_gender:String;

        public function LocalizedString(_arg1:String, _arg2:Object=null){
            this.m_original = _arg1;
            this.m_variations = _arg2;
            this.m_gender = null;
        }
        public function set gender(_arg1:String):void{
            this.m_gender = _arg1;
        }
        public function get gender():String{
            return (this.m_gender);
        }
        public function toString(_arg1:Object, _arg2:SubstituterSimple):String{
            var _local4:String;
            var _local5:String;
            var _local6:Object;
            var _local7:int;
            var _local8:String;
            var _local9:Array;
            var _local10:Array;
            var _local11:LocalizationToken;
            var _local12:int;
            var _local13:Array;
            var _local14:Array;
            var _local15:Array;
            var _local16:Array;
            var _local17:String;
            var _local18:Number;
            var _local3:Array = new Array();
            for (_local4 in this.m_variations) {
                _local3.push(_local4);
            };
            if (_local3.length == 0){
                _local3.push(ORIGINAL);
            };
            _local6 = {};
            for (_local17 in _arg1) {
                if ((_arg1[_local17] is LocalizationToken)){
                    _local11 = (_arg1[_local17] as LocalizationToken);
                    _local13 = [];
                    _local14 = PROPAGATED_ATTRIBUTES;
                    _local12 = 0;
                    _local7 = 0;
                    while (_local7 < _local3.length) {
                        _local8 = this.getVariation(_local3[_local7]);
                        _local9 = _local8.match(new RegExp((("{" + _local17) + "(?:,\\s*([a-z,\\s*]+))?}")));
                        if (((_local9) && ((_local9.length == 2)))){
                            _local12++;
                            if ((_local9[1] is String)){
                                _local10 = _local9[1].split(",");
                                _local18 = 0;
                                while (_local18 < _local10.length) {
                                    _local10[_local18] = _local10[_local18].replace(/^\s+|\s+$/g, "");
                                    _local18++;
                                };
                                _local14 = Utilities.arrayIntersect(_local14, _local10);
                                _local13.push(_local10);
                            };
                        };
                        _local7++;
                    };
                    if (_local12 > 0){
                        if (_local13.length > 0){
                            _local11.addAttributes(_local14);
                            _local15 = _local11.filterIndexes(_local13);
                            _local16 = [];
                            _local7 = 0;
                            while (_local7 < _local15.length) {
                                _local16.push(_local3[_local15[_local7]]);
                                _local7++;
                            };
                            _local3 = _local16;
                        };
                        _local6[_local17] = _local11.getString();
                    };
                } else {
                    _local6[_local17] = _arg1[_local17].toString();
                };
            };
            if (_local3.length > 1){
                throw (new Error("Ambiguous token specification for string, please provide more token attributes to narrow your selection."));
            };
            if (_local3.length < 1){
                throw (new Error("No matches found."));
            };
            _local5 = this.getVariation(_local3[0]).replace(new RegExp("{([^},]+),\\s*[a-z0-9,\\s*]+}", "g"), "{$1}");
            return (_arg2.replace(_local5, _local6));
        }
        public function getVariation(_arg1:String):String{
            if (_arg1 == ORIGINAL){
                return (this.m_original);
            };
            if (this.m_variations[_arg1]){
                return (this.m_variations[_arg1]);
            };
            return ("false");
        }

    }
}//package ZLocalization 
